// pages/perCenter/withdLast/withdLast.js
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      divided:null,
      maxDivided:null,
      tx: false,
      txInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      divided: options.divided, 
      maxDivided:options.divided
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
  fnSubmitBank:function(){
    var that = this;
    wx.navigateTo({
      url: '/pages/perCenter/withdraw/withdraw?divided='+that.data.divided,
    })
  },
  //输入检查
  fnInputDivided:function(e){
    var that = this;
    var divided = 0;
    if(e.detail.value<0){
      divided = 0.01;
    }else if(e.detail.value>that.data.maxDivided){
      divided = that.data.maxDivided;
    }else{
      divided = e.detail.value;
    }
    that.setData({
      divided: divided
    })
  },
  fnClear:function(){
    this.setData({
      divided:""
    })
  },
  fnTx: function (e) {
    var that = this;
    var shareMoney = that.data.divided;
    if (!shareMoney || shareMoney == 0) {
      wx.showModal({
        title: '无可用提现额度',
        content: '',
      })
      return;
    }
    var txInfo = wx.getStorageSync("txInfo");
    that.setData({
      tx: true,
      txInfo: txInfo == '' ? {} : txInfo,
      price: shareMoney
    })
  }, 
  fnSureTx: function (e) {
    var that = this;
    var txInfo = that.data.txInfo;
    if (txInfo == null || txInfo.account == null || txInfo.account == "" || txInfo.accountName == null || txInfo.accountName == "" || that.data.price == '' || that.data.price == null || txInfo.remark == '' || txInfo.remark == null) {
      var msg = '请完善提现信息！'
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    if (!util.checkBankNo(txInfo.account)) {
      msg = "银行账户无效!"
      wx.showModal({
        title: '提示',
        content: msg,
      })
      return;
    }
    wx.setStorageSync("txInfo", txInfo);
    if (parseFloat(this.data.divided) - parseFloat(this.data.price) < 0) {
      return;
    }
    var data = {
      price: this.data.price,
      account: this.data.txInfo.account,
      accountName: this.data.txInfo.accountName,
      remark: this.data.txInfo.remark,
      token: wx.getStorageSync("token")
    }
    wx.showLoading({
      title: '加载中。。。',
      mask: true
    })
    util.request(api.Tx, data).then(function (res) {
      wx.hideLoading();
      if (res.code === '200') {
        that.setData({
          tx: false,
          divided: res.datas
        });
       wx.navigateBack({});
      }else{
        wx.showToast({
          title: res.message,
        })
      }
    });
},
  fnInputTxInfo: function (e) {
    var id = e.currentTarget.id;
    var txInfo = this.data.txInfo;
    txInfo['' + id] = e.detail.value;
    this.setData({
      txInfo: txInfo,
    });
    if ("divided" == id) {
      var price = e.detail.value;
      if (parseFloat(this.data.divided) - parseFloat(e.detail.value) < 0 || price < 0) {
        price = this.data.divided;
      }
      this.setData({
        price: price
      })
    }
  }
})